from django.db import models
from users.models import User
from goods.models import Good
# Create your models here.

# 收货地址模型类
class Addr(models.Model):
    receiver = models.CharField('收货人',max_length=20)
    receiver_mobile = models.CharField('手机号',max_length=11)
    receiver_addr = models.CharField('收货地址',max_length=100)
    user = models.ForeignKey(User,on_delete=models.CASCADE,verbose_name='所属用户')

    def __str__(self):
        return '收件人:%s'%self.receiver

    class Meta:
        db_table = 'addr'
        verbose_name_plural = '收货地址'


# 订单模型类
class OrderInfo(models.Model):
    PAY_METHOD = (
        (1,'支付宝'),
        (2,'微信支付'),
        (3,'银联支付'),
        (4,'货到付款')
    )
    PAY_STATUS = (
        (1,'待支付'),
        (2,'待发货'),
        (3,'待收货'),
        (4,'待评价'),
        (5,'已完成'),
    )
    order_id = models.CharField('订单编号',max_length=64,primary_key=True)
    user = models.ForeignKey(User,on_delete=models.CASCADE,verbose_name='下单用户',related_name='orders')
    addr = models.ForeignKey(Addr,on_delete=models.CASCADE,verbose_name='收货地址')
    total_count = models.IntegerField('商品总数',default=1)
    total_amount = models.DecimalField('总价格',max_digits=10,decimal_places=2)
    freight = models.DecimalField('邮费',max_digits=10,decimal_places=2)
    pay_method = models.SmallIntegerField('支付方式',choices=PAY_METHOD,default=1)
    status = models.SmallIntegerField('支付状态',choices=PAY_STATUS,default=1)
    created_time = models.DateTimeField('订单创建时间',auto_now_add=True)

    def __str__(self):
        return self.order_id

    class Meta:
        db_table = 'order_info'
        verbose_name_plural = '订单信息'


# 订单商品表
class OrderGoods(models.Model):
    sku = models.ForeignKey(Good,on_delete=models.CASCADE,verbose_name='商品')
    count = models.IntegerField('购买的数量',default=1)
    price = models.DecimalField('价格',max_digits=10,decimal_places=2)
    order = models.ForeignKey(OrderInfo,on_delete=models.CASCADE,verbose_name='所属订单',related_name='orderGoods')
    comment = models.TextField('评论信息',default='')
    score = models.SmallIntegerField('评分',default=5)
    is_anonymous = models.BooleanField('是否匿名评价',default=False)
    is_commented = models.BooleanField('是否评价',default=False)

    def __str__(self):
        return '%s:%s'%(self.order.order_id,self.sku)

    class Meta:
        db_table = 'orderGoods'
        verbose_name_plural = '订单商品'


# 支付信息
class PaymentInfo(models.Model):
    trade_id = models.CharField('支付编号',max_length=100,unique=True,null=True,blank=True)
    order = models.ForeignKey(OrderInfo,on_delete=models.CASCADE,verbose_name='订单')

    class Meta:
        db_table = 'payment_info'
        verbose_name_plural = '支付信息'

    def __str__(self):
        return self.trade_id

